package com.care.dao.consult;

import java.util.List;

import javax.annotation.Resource;
import javax.print.Doc;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.User;
/**
 * 
 * @author jiangxiongjie
 *	2020年3月19日 上午11:59:47
 */
@Repository
public class ConsultDaoImpl {
  @Resource
  private SessionFactory sessionFactory;
  public Session getSession(){
	  return this.sessionFactory.getCurrentSession();
  }
  /**
   * 根据dc查询consult表
   * @param doctor
   * @param pageNum
   * @param pageSize
   * @return
   */
  public List<Consult> findConsultBydc(Doctor doctor,int pageNum,int pageSize){
	  String hql="from Consult where dc=?";
	  Query query=getSession().createQuery(hql);
	  query.setParameter(0, doctor.getId());
	  query.setFirstResult((pageNum-1)*pageSize);
	  query.setMaxResults(pageSize);	  
	  return query.list();	  
  }
  /**
   * 计算consult表dc数据总数
   * @param doctor
   * @return
   */
  public int countCoultBydc(Doctor doctor){
	  String hql="select count(*) from Consult where dc=?";
	  int count=((Long)this.getSession().createQuery(hql).setParameter(0, doctor.getId()).setCacheable(true).uniqueResult()).intValue();
	  return count;	  
  }
  /**
   * 根据uc查询consult表
   * @param doctor
   * @param pageNum
   * @param pageSize
   * @return
   */
  public List<Consult> findConsultByuc(User user,int pageNum,int pageSize){
	  String hql="from Consult where uc=?";
	  Query query=getSession().createQuery(hql);
	  query.setParameter(0, user.getId());
	  query.setFirstResult((pageNum-1)*pageSize);
	  query.setMaxResults(pageSize);	  
	  return query.list();	  
  }
  /**
   * 计算consult表uc数据总数
   * @param doctor
   * @return
   */
  public int countCoultByuc(User user){
	  String hql="select count(*) from Consult where uc=?";
	  int count=((Long)this.getSession().createQuery(hql).setParameter(0, user.getId()).setCacheable(true).uniqueResult()).intValue();
	  return count;	  
  }
  /**
   * 判断修改的金额
   * 并更改用户金额
   * 修改咨询表金额显示
   * @param consult
   * @param user
   */
  public boolean updateConsult(Consult consult,User user,int id){
	  String hql1="select money from Consult where id=?";
	  
	  double consultMoney=(double) this.getSession().createQuery(hql1).setParameter(0,id).uniqueResult();
	  String hql="select money from User where email=?";
	  double userMoney=(double) this.getSession().createQuery(hql).setParameter(0, user.getEmail()).uniqueResult();	 
	  String hql6="update Consult set money=?,content=? where id=?";
	  this.getSession().createQuery(hql6).setParameter(0, consult.getMoney()).setParameter(1, consult.getContent()).setParameter(2, id).executeUpdate();
	  if (consult.getMoney()>consultMoney) {
		double  more=consult.getMoney()-consultMoney;
		if (userMoney>more) {
		
			String hql2="update User set money=money-? where email=?";
			this.getSession().createQuery(hql2).setParameter(0, more).setParameter(1, user.getEmail()).executeUpdate();
			
			String hql5="select money from Consult where id=?";			  
			double consultMoney1=(double) this.getSession().createQuery(hql5).setParameter(0,id).uniqueResult();
			System.out.println("操作成功，将扣除金额"+more+"元，待认证失败将会返还"+consultMoney1+"元");
			return true;
			}	
			else {
				return false;
			}
		}
	  else if (consult.getMoney()<consultMoney) {		
		 double less=consultMoney-consult.getMoney();
		String hql3="update User set money=money+? where email=?";
		this.getSession().createQuery(hql3).setParameter(0, less).setParameter(1, user.getEmail()).executeUpdate();
		String hql5="select money from Consult where id=?";			  
		double consultMoney1=(double) this.getSession().createQuery(hql5).setParameter(0,id).uniqueResult();
		System.out.println("操作成功，将退还金额"+less+"元，待认证失败将会返还"+consultMoney1+"元");
		return true;
	}	
		  return false;				 	  
  }
  /**
   * 用户聊天功能(修改content实现)
   * @param consult
   * @param id
   */
  public void updateConsultUser(Consult consult,int id){
	  String hql="update Consult set content=? where id=?";
	  this.getSession().createQuery(hql).setParameter(0, consult.getContent()).setParameter(1, id).executeUpdate();	  
  }
  
  /**
   * 医生聊天功能(修改answer实现)
   * @param consult
   * @param id
   */
  public void updateConsultDoctor(Consult consult,int id){
	  String hql="update Consult set answer=? where id=?";
	  this.getSession().createQuery(hql).setParameter(0, consult.getAnswer()).setParameter(1, id).executeUpdate();	  
  }
  /**
   * 诊断为病人
   * @param consult
   * @param doctor
   * @param id
   */
  public void updateConsultDoctorMoney(Consult consult,Doctor doctor,int id){
	  String hql="select money from Consult where id=?";
	  double money=(double) this.getSession().createQuery(hql).setParameter(0, id).uniqueResult();
	  String hql1="select money from Doctor where id=?";
	  this.getSession().createQuery(hql1).setParameter(0, doctor.getId()).uniqueResult();
	  String hql3="update Doctor set money=money+? where id=?";
	  this.getSession().createQuery(hql3).setParameter(0, money).setParameter(1, doctor.getId()).executeUpdate();
	  String hql2="update Consult set money=?,type=?,patient=?,answer=? where id=?";
	  Query query=getSession().createQuery(hql2);
	  query.setParameter(0, 0.0);
	  query.setParameter(1, consult.getType());
	  query.setParameter(2, true);
	  query.setParameter(3, consult.getAnswer());
	  query.setParameter(4, id);
	  query.executeUpdate();	  
  }
  /**
   * 诊断后无病
   * @param consult
   * @param user
   * @param id
   */
  public void updateConsultDoctorPatient(Consult consult,String email,int id){
	  String hql="select money from Consult where id=?";
	  double money=(double) this.getSession().createQuery(hql).setParameter(0, id).uniqueResult();
	  String hql3="update User set money=money+? where email=?";
	  this.getSession().createQuery(hql3).setParameter(0, money).setParameter(1, email).executeUpdate();	 
	  String hql2="update Consult set money=?,type=?,patient=?,answer=? where id=?";
	  Query query=getSession().createQuery(hql2);
	  query.setParameter(0, 0.0);
	  query.setParameter(1, null);
	  query.setParameter(2, false);
	  query.setParameter(3, consult.getAnswer());
	  query.setParameter(4, id);
	  query.executeUpdate();
  }
}
